﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL.Managers;
using BLL;

namespace CorporateGymManagementSystem
{
    public delegate void AddNewTraineeEventHandler();

    /// <summary>
    /// Interaction logic for NewTraineeCreationWindow.xaml
    /// </summary>
    public partial class NewTraineeCreationWindow : Window
    {
        public event AddNewTraineeEventHandler NewTraineeAdded;
        private RadioButton workerTypeRadioButtonChecked;
        private Trainee m_Trainee = null;
        private bool isEdit;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="i_Trainee">if not null, show edit options</param>
        public NewTraineeCreationWindow(Trainee i_Trainee)
        {
            isEdit = true;
            InitializeComponent();
            resultLabel.Content = string.Empty;
            if (i_Trainee != null)
            {
                m_Trainee = i_Trainee;
                firstNameTextBox.Text = i_Trainee.FirstName;
                lastNameTextBox.Text = i_Trainee.LastName;
                phoneTextBox.Text = i_Trainee.Phone;
                mailTextBox.Text = i_Trainee.Email;
                employeeIdTextBox.Text = i_Trainee.WorkerId.ToString();
                MedicalApproval approval = MedicalApprovalManager.Instance.GetMedicalApprovalById(i_Trainee.MedicalApprovalId);
                MedicalApprovalExpirationDatePicker.SelectedDate = approval.ExpirationDate;
                if (i_Trainee.WorkerType == EWorkerType.Contractor)
                {
                    radionButtonContactor.IsChecked = true;
                }
                else
                {
                    radionButtonOutsource.IsChecked = true;
                }
            }
            else
            {
                //m_Trainee = new Trainee();
            }
        }

        public NewTraineeCreationWindow()
        {
            isEdit = false;
            InitializeComponent();
            
            m_Trainee = new Trainee();
        }

        private void radioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton ck = sender as RadioButton;
            if (ck.IsChecked.Value)
                workerTypeRadioButtonChecked = ck;
        }

        private bool IsTextValid(TextBox control, out string text)
        {
            text = string.Empty;
            if (string.IsNullOrWhiteSpace(control.Text) == false)
            {

                control.Background = Brushes.White;
                text = control.Text;
            }

            else
            { 
                control.Background = Brushes.Pink;
            }

            return text != string.Empty;
        }

        private void EditTraineeButton_Click(object sender, RoutedEventArgs e)
        {
            resultLabel.Content = string.Empty;
        }

        private void addTraineeButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                resultLabel.Content = string.Empty;
                bool isAllValid = true;
                string firstName, lastName, workerId, email, phone;
                DateTime medicalApprovalExpirationDate = DateTime.Now;
                isAllValid &= IsTextValid(firstNameTextBox, out firstName);
                isAllValid &= IsTextValid(lastNameTextBox, out lastName);
                isAllValid &= IsTextValid(employeeIdTextBox, out workerId);
                isAllValid &= IsTextValid(mailTextBox, out email);
                isAllValid &= IsTextValid(phoneTextBox, out phone);


                EWorkerType workerType = workerTypeRadioButtonChecked.Name.Equals("radionButtonContactor") ? EWorkerType.Contractor : EWorkerType.OutSource;
                if (MedicalApprovalExpirationDatePicker.SelectedDate.HasValue == false)
                {
                    isAllValid = false;
                    MedicalApprovalExpirationDatePicker.Background = Brushes.Pink;
                }
                else
                {
                    MedicalApprovalExpirationDatePicker.Background = Brushes.White;
                    medicalApprovalExpirationDate = MedicalApprovalExpirationDatePicker.SelectedDate.Value;
                }

                if (isAllValid == false)
                {
                    resultLabel.Content = "You must fill all the fields properly!";
                    resultLabel.Foreground = Brushes.Crimson;
                    return;
                }

                if (m_Trainee.MedicalApprovalId == 0)
                {
                    int medicalApprovalId = MedicalApprovalManager.Instance.AddMedicalApproval(medicalApprovalExpirationDate);
                    if (medicalApprovalId == 0)
                    {
                        resultLabel.Content = "Cannot Create Medical Approval!";
                        resultLabel.Foreground = Brushes.Crimson;
                    }
                    else
                    {
                        m_Trainee.MedicalApprovalId = medicalApprovalId;
                    }
                }

                m_Trainee.FirstName = firstName;
                m_Trainee.LastName = lastName;
                m_Trainee.Email = email;
                m_Trainee.Phone = phone;
                m_Trainee.WorkerId = workerId;
                m_Trainee.WorkerType = workerType;

                if (isEdit)
                {
                    bool success = TraineeManager.Instance.UpdateTrainee(m_Trainee);
                    if (success)
                    {
                        NewTraineeAdded();
                        resultLabel.Content = "Trainee was successfuly edited!";
                        resultLabel.Foreground = Brushes.Green;
                    }
                    else
                    {
                        resultLabel.Content = "Trainee edit failed!";
                        resultLabel.Foreground = Brushes.Crimson;
                    }
                }
                else
                {
                    int traineeId = TraineeManager.Instance.AddTrainee(m_Trainee);
                    if (traineeId == 0)
                    {
                        resultLabel.Content = "Trainee add failed!";
                        resultLabel.Foreground = Brushes.Crimson;
                    }

                    else
                    {
                        NewTraineeAdded();
                        resultLabel.Content = "Trainee was successfuly added!";
                        resultLabel.Foreground = Brushes.Green;
                        this.Close();
                    }
                }
                
            }

            catch (Exception ex)
            {
                resultLabel.Content = "Error!!";
                resultLabel.Foreground = Brushes.Crimson;
            }
        }
    }
}
